/*
 * @description: 
 * @Author: latiao
 * @Date: 2022-06-15 16:25:22
 * @LastEditors: latiao
 * @LastEditTime: 2022-09-16 15:13:22
 */
import Axios from "./axios";
import storage from "@/libs/storage";
import {Message} from "element-ui";
import store from "@/store";

const api = new Axios({
    baseURL: process.env.VUE_APP_BASEURL,
});

api.instance.interceptors.request.use(
    config => {
        config.headers.Accept = "application/json";
        config.headers.Contenttype = "application/json";
        config.headers.version = "2.0";
        config.headers.token = storage.get("token") || store.state.user.token;
        if (store.state.user.userInfo && store.state.user.userInfo.shopInfoVo) {
            config.headers.shopId = store.state.user.userInfo.shopInfoVo.shopId;
            config.headers.tenantId = store.state.user.userInfo.shopInfoVo.tenantId;
        }
        return config;
    },
    err => {
        Message.error({
            showClose: true,
            message: err,
            duration: 1500,
        });
        return Promise.reject(err);
    },
);

api.instance.interceptors.response.use(
    res => {
        const {code} = res.data;
        if (code !== 200) {
            return Promise.reject(
                res.data.msg || res.data.message || "未知错误，请稍后再试",
            );
        }
        return res;
    },

    err => {
        Message.error({
            showClose: true,
            message: err,
            duration: 1500,
        });
        return Promise.reject(err);
    },
);
export default api;
